package com.sg.business.commons.operation.handler.vault;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.handlers.HandlerUtil;

import com.sg.business.commons.ui.vault.IVaultControlPart;
import com.sg.business.model.Folder;
import com.sg.widgets.MessageUtil;

public abstract class AbstractFolderHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IStructuredSelection sel = (IStructuredSelection) HandlerUtil
				.getCurrentSelection(event);
		if (sel == null || sel.isEmpty()) {
			return null;
		}
		Object element = sel.getFirstElement();
		if (!(element instanceof Folder)) {
			return null;
		}

		final Folder folder = (Folder) element;

		try {
			IWorkbenchPart part = HandlerUtil.getActivePart(event);
			Shell shell = HandlerUtil.getActiveShell(event);
			IVaultControlPart vaultControlPart = null;
			if (part instanceof IVaultControlPart) {
				vaultControlPart = (IVaultControlPart) part;
			}
			run(folder, vaultControlPart, shell);

		} catch (Exception e) {
			MessageUtil.showToast(e);
		}

		return null;
	}

	protected abstract void run(Folder folder,
			IVaultControlPart vaultControlPart, Shell shell);

}